//
//  TabBarButton.m
//  MoodDiary
//
//  Created by MoodDiary on 2020/11/26.
//

#import "TabBarButton.h"

@implementation TabBarButton

- (void)setTabBarItem:(UITabBarItem *)tabBarItem {
    _tabBarItem = tabBarItem;
    
    @weakify(self)
    
    if (tabBarItem.title != nil && tabBarItem.title.length > 0) {
        [RACObserve(tabBarItem, title) subscribeNext:^(id x) {
            @strongify(self)
            [self valueChanged];
        }];
    }
    
    [RACObserve(tabBarItem, image) subscribeNext:^(id x) {
        @strongify(self)
        [self valueChanged];
    }];
    [RACObserve(tabBarItem, selectedImage) subscribeNext:^(id x) {
        @strongify(self)
        [self valueChanged];
    }];
}

- (void)setHighlighted:(BOOL)highlighted {
    // 重写高亮 覆盖原有方法 来干掉按钮按住不松手时 图标变会的效果
}

- (void)valueChanged {
    [self setTitle:_tabBarItem.title forState:UIControlStateNormal];
    
    [self setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
    
    [self setImage:_tabBarItem.image forState:UIControlStateNormal];
    [self setImage:_tabBarItem.selectedImage forState:UIControlStateSelected];
}

@end
